<?php
namespace App\Services\Base;

use App\Exceptions\Response\Service\ServiceException;
use App\Exceptions\Response\Service\ServiceCode;
use App\Models\Base\UserModel;
use App\Services\Postman\CoreService as PostmanCoreService;

class UserService
{

    /**
     * 获取登录用户数据（id）
     *
     * @param int $id            
     * @throws ServiceException
     * @return array
     */
    public function findUserById($id)
    {
        $user = UserModel::findById($id)->shift();
        if (! $user) {
            throw new ServiceException(ServiceCode::SERVICE_NOT_EXIST_USER);
        }
        return $user;
    }

    /**
     * 获取登录用户数据（mobile）
     *
     * @param string $mobile            
     * @throws ServiceException
     * @return array
     */
    public function findUserByMobile($mobile)
    {
        $user = UserModel::findByMobile($mobile)->shift();
        if (! $user) {
            throw new ServiceException(ServiceCode::SERVICE_NOT_EXIST_USER);
        }
        return $user;
    }

    /**
     * 获取用户token（核心服务）
     *
     * @param string $mobile            
     * @param string $password            
     * @return array
     */
    public function getUserToken($mobile, $password)
    {
        $data['mobile'] = $mobile;
        $data['password'] = $password;
        
        $user = PostmanCoreService::login($data);
        if (! $user) {
            throw new ServiceException(ServiceCode::SERVICE_NOT_EXIST_USER);
        }
        
        return $user;
    }
}